package main

import (
	"flag"
	"net/http"
	"os"
	"strings"

	"github.com/golang/glog"
)

func ReadUserIP(r *http.Request) string {
	IPAddress := r.Header.Get("X-Real-Ip")
	if IPAddress == "" {
		IPAddress = r.Header.Get("X-Forwarded-For")
	}
	if IPAddress == "" {
		IPAddress = strings.Split(r.RemoteAddr, ":")[0]
	}
	return IPAddress
}

func healthz(w http.ResponseWriter, req *http.Request) {
	for name, headers := range req.Header {
		w.Header()[name] = headers
	}
	w.Header()["VERSION"] = []string{os.Getenv("VERSION")}
	glog.Infof("GET healthz: client_ip=%s, status_code=%d", ReadUserIP(req), 200)
}

func init() {
	flag.Set("logtostderr", "true")
	flag.Parse()
}

func main() {
	http.HandleFunc("/healthz", healthz)

	http.ListenAndServe(":8000", nil)
}
